<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon :icon-class="isScreenFull? 'exit-fullscreen': 'fullscreen'" class="fullscreen" @click="toggleScreen" />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      isScreenFull: false
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', () => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreen
      if (!isFull) {
        this.isScreenFull = false
      }
    })
  },
  methods: {
    toggleScreen() {
      // 如果未开启就开启 如果已开启就关闭
      if (this.isScreenFull) {
        document.exitFullscreen()
      } else {
        document.documentElement.requestFullscreen()
      }
      this.isScreenFull = !this.isScreenFull
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
